package edu.uoc.tdp.pac4.client;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


import edu.uoc.tdp.pac4.beans.Rol;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

public class Client extends JFrame {
	
	
	private static final long serialVersionUID = 1L;


	ClientConnectionHelper connectionHelper = null;



	public Client(ClientConnectionHelper connection) {
		super(LanguageUtils.getMessage("main.window.title"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setPreferredSize(new Dimension(700,500));
		setResizable(true);

		
		connectionHelper = connection;
		
	
		buildMenu();
		
		pack();
	}
	
	JMenu mnConnectionRegistration;
	JMenuItem mntmSubscriptionRegistration;
	JMenuItem mntmCheckRegistration;
	JMenuItem mntmScheduleAttendance;
	JMenuItem mntmExit;
	JMenu mnAcademicManagement;
	JMenuItem mntmGroupsManagement;
	JMenuItem mntmRegistrationsManagement;
	JMenuItem mntmAssistanceManagement;
	JMenu mnMaintenance;
	JMenuItem mnUsersMaintenance;
	JMenuItem mnClassesMaintenance;
	JMenuItem mnCoursesManagement;
	JMenu mnStatistics;
	JMenuItem mntmStudent;
	JMenuItem mntmTeacher;
	JMenuItem mntmStaff;
	
	private void buildMenu(){
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnConnectionRegistration = new JMenu(LanguageUtils.getMessage("main.menu.connection"));
		menuBar.add(mnConnectionRegistration);
		

		
		mntmSubscriptionRegistration = new JMenuItem(LanguageUtils.getMessage("main.menu.subscription.language"));
		mntmSubscriptionRegistration.setActionCommand("main.menu.subscription.language");
		mntmSubscriptionRegistration.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmSubscriptionRegistration.setEnabled(true);
		mnConnectionRegistration.add(mntmSubscriptionRegistration);
		
		boolean enabled = false;
		if(ClientConnectionHelper.getLoggedUser() != null && ClientConnectionHelper.getLoggedUser().getRol() == Rol.Alumne){
			enabled = true;
		}
		mntmCheckRegistration = new JMenuItem(LanguageUtils.getMessage("main.menu.check.registration"));
		mntmCheckRegistration.setActionCommand("main.menu.check.registration");
		mntmCheckRegistration.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmCheckRegistration.setEnabled(enabled);
		mnConnectionRegistration.add(mntmCheckRegistration);
		
		
		mntmScheduleAttendance = new JMenuItem(LanguageUtils.getMessage("main.menu.schedule.attendance"));
		mntmScheduleAttendance.setActionCommand("main.menu.schedule.attendance");
		mntmScheduleAttendance.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmScheduleAttendance.setEnabled(enabled);
		mnConnectionRegistration.add(mntmScheduleAttendance);
		
		mntmExit = new JMenuItem(LanguageUtils.getMessage("main.menu.exit"));
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 System.exit(0);    
			}
		});
		mnConnectionRegistration.add(mntmExit);
		
		mnAcademicManagement = new JMenu(LanguageUtils.getMessage("main.menu.management"));
		menuBar.add(mnAcademicManagement);
		
		enabled = false;
		if(ClientConnectionHelper.getLoggedUser() != null && (ClientConnectionHelper.getLoggedUser().getRol() == Rol.Personal_Academic || ClientConnectionHelper.getLoggedUser().getRol() == Rol.Administrador) ){
			enabled = true;
		}
		mntmGroupsManagement = new JMenuItem(LanguageUtils.getMessage("main.menu.management.groups"));
		mnAcademicManagement.add(mntmGroupsManagement);
		mntmGroupsManagement.setActionCommand("main.menu.groups");
		mntmGroupsManagement.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmGroupsManagement.setEnabled(enabled);

		enabled = false;
		if(ClientConnectionHelper.getLoggedUser() != null && ( ClientConnectionHelper.getLoggedUser().getRol() == Rol.Personal_Academic  || ClientConnectionHelper.getLoggedUser().getRol() == Rol.Administrador)){
			enabled = true;
		}
		mntmRegistrationsManagement = new JMenuItem(LanguageUtils.getMessage("main.menu.management.registrations"));
		mnAcademicManagement.add(mntmRegistrationsManagement);
		mntmRegistrationsManagement.setActionCommand("main.menu.registrations");
		mntmRegistrationsManagement.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmRegistrationsManagement.setEnabled(enabled);

		enabled = false;
		if(ClientConnectionHelper.getLoggedUser() != null && ClientConnectionHelper.getLoggedUser().getRol() == Rol.Professor){
			enabled = true;
		}
		mntmAssistanceManagement = new JMenuItem(LanguageUtils.getMessage("main.menu.management.assistance"));
		mnAcademicManagement.add(mntmAssistanceManagement);
		mntmAssistanceManagement.setActionCommand("main.menu.attendance");
		mntmAssistanceManagement.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmAssistanceManagement.setEnabled(enabled);
		
		// Maintenance
		mnMaintenance = new JMenu(LanguageUtils.getMessage("main.menu.maintenance"));
		menuBar.add(mnMaintenance);

		// user
		mnUsersMaintenance = new JMenuItem(LanguageUtils.getMessage("main.menu.maintenance.user"));
		mnUsersMaintenance.setActionCommand("main.menu.user.list");
		mnUsersMaintenance.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mnMaintenance.add(mnUsersMaintenance);
		
		// classroom
		mnClassesMaintenance = new JMenuItem(LanguageUtils.getMessage("main.menu.maintenance.class"));
		mnClassesMaintenance.setActionCommand("main.menu.classroom.list");
		mnClassesMaintenance.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mnMaintenance.add(mnClassesMaintenance);
		
		// Course
		mnCoursesManagement = new JMenuItem(LanguageUtils.getMessage("main.menu.maintenance.course"));
		mnCoursesManagement.setActionCommand("main.menu.course.list");
		mnCoursesManagement.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mnMaintenance.add(mnCoursesManagement);
		
		if(ClientConnectionHelper.getLoggedUser() == null || ClientConnectionHelper.getLoggedUser().getRol() != Rol.Administrador){
			mnUsersMaintenance.setEnabled(false);
			mnClassesMaintenance.setEnabled(false);
			mnCoursesManagement.setEnabled(false);			
		}
		
		
		mnStatistics = new JMenu(LanguageUtils.getMessage("main.menu.statistics"));
		menuBar.add(mnStatistics);
		
		enabled = false;
		if(ClientConnectionHelper.getLoggedUser() != null && ClientConnectionHelper.getLoggedUser().getRol() == Rol.Alumne){
			enabled = true;
		}
		mntmStudent = new JMenuItem(LanguageUtils.getMessage("main.menu.statistics.student"));
		mntmStudent.setActionCommand("main.menu.statistics.student");
		mntmStudent.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmStudent.setEnabled(enabled);
		mnStatistics.add(mntmStudent);
		
		enabled = false;
		if(ClientConnectionHelper.getLoggedUser() != null && ClientConnectionHelper.getLoggedUser().getRol() == Rol.Professor){
			enabled = true;
		}
		mntmTeacher = new JMenuItem(LanguageUtils.getMessage("main.menu.statistics.teacher"));
		mntmTeacher.setActionCommand("main.menu.statistics.teacher");
		mntmTeacher.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmTeacher.setEnabled(enabled);
		mnStatistics.add(mntmTeacher);
		
		enabled = false;
		if(ClientConnectionHelper.getLoggedUser() != null && ClientConnectionHelper.getLoggedUser().getRol() == Rol.Personal_Academic){
			enabled = true;
		}
		mntmStaff = new JMenuItem(LanguageUtils.getMessage("main.menu.statistics.staff"));
		mntmStaff.setActionCommand("main.menu.statistics.staff");
		mntmStaff.addActionListener(new ClientMenuActionListener(this, connectionHelper));
		mntmStaff.setEnabled(enabled);
		mnStatistics.add(mntmStaff);
	}
	
	
	public void repaint() {
		
		
		mnConnectionRegistration.setText(LanguageUtils.getMessage("main.menu.connection"));
		mntmSubscriptionRegistration.setText(LanguageUtils.getMessage("main.menu.subscription.language"));
		mntmCheckRegistration.setText(LanguageUtils.getMessage("main.menu.check.registration"));
		mntmScheduleAttendance.setText(LanguageUtils.getMessage("main.menu.schedule.attendance"));
		mntmExit.setText(LanguageUtils.getMessage("main.menu.exit"));
		mnAcademicManagement.setText(LanguageUtils.getMessage("main.menu.management"));
		mntmGroupsManagement.setText(LanguageUtils.getMessage("main.menu.management.groups"));
		mntmRegistrationsManagement.setText(LanguageUtils.getMessage("main.menu.management.registrations"));
		mntmAssistanceManagement.setText(LanguageUtils.getMessage("main.menu.management.assistance"));
		mnMaintenance.setText(LanguageUtils.getMessage("main.menu.maintenance"));
		mnUsersMaintenance.setText(LanguageUtils.getMessage("main.menu.maintenance.user"));
		mnClassesMaintenance.setText(LanguageUtils.getMessage("main.menu.maintenance.class"));
		mnCoursesManagement.setText(LanguageUtils.getMessage("main.menu.maintenance.course"));
		mnStatistics.setText(LanguageUtils.getMessage("main.menu.statistics"));
		mntmStudent.setText(LanguageUtils.getMessage("main.menu.statistics.student"));
		mntmTeacher.setText(LanguageUtils.getMessage("main.menu.statistics.teacher"));
		mntmStaff.setText(LanguageUtils.getMessage("main.menu.statistics.staff"));
		super.pack();
		super.repaint();
		super.repaint();
	}
	
		
}
